<?php

class bulletin_widget
{
    private $col_arr = array(
        'A',
        'B',
        'C',
        'D',
        'E',
        'F',
        'G',
        'H',
        'I',
        'J',
        'K',
        'L',
        'M',
        'N',
        'O',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'U',
        'V',
        'W',
        'X',
        'Y',
        'Z',
        'AA',
        'AB',
        'AC',
        'AD',
        'AE',
        'AF',
        'AG',
        'AH',
        'AI',
        'AJ',
        'AK',
        'AL',
        'AM',
        'AN'
    );
    private static $set_up_time = '2016-02-18 23:59:59';
    private $enable_user_ids = array();
    private $enable_store_ids = array();
    private $dir = '';
    private $path = '';

    public function __construct() {
        $this->dir = EXPORT_PATH;
    }

    /**
     *
     * 存入数据表
     */
    public function save_partner_bulletin(){
        // 获取所有有效的经销商
        $partner_ids = user_helper::get_role_user_ids(user_config::AGENCY, '', array('is_enable' => 1, 'status' => 1));
        if (!$partner_ids) {
            return array();
        }
        $partner_lists = array();
        foreach ($partner_ids as $k => $v) {
            $cur_dealer_info = user_helper::get_user_info($v);
            //合伙人ID
            $partner_lists['partner_id'] = $cur_dealer_info['id'];
            //合伙人状态
            $partner_lists['is_enable'] = $cur_dealer_info['is_enable'];
            // 新增合伙人姓名
            $partner_lists['partner_name'] = $cur_dealer_info['realname'] ? $cur_dealer_info['realname'] : $cur_dealer_info['nickname'];
            //合伙人所在省份和所在城市编码
            $dealer_address = dealers_helper::get_default_dealer_address($v);
            if($dealer_address){
                $partner_lists['province_code'] = $dealer_address['province_code'];
                $partner_lists['city_code'] = $dealer_address['city_code'];
            }else{
                $partner_lists['province_code'] = "000000";
                $partner_lists['city_code'] = "000000";
            }

            //所属大区经理ID
            $partner_lists['area_manager_id'] = $cur_dealer_info['area_manager_id'] ? $cur_dealer_info['area_manager_id'] : "0";
            // 掌柜数量
            $store_list = store_helper::get_store_list_by_dealer_id($cur_dealer_info['id'], array('is_enable' => 1, 'is_delete'=>1, 'status' => array(1, 2)));
            $partner_lists['store_num'] = count($store_list);
            $is_ever_stock = array();
            $stock_num  = array();
            $sell_num  = array();
            foreach($store_list as $ke => $va){
                $ever_stock = store_helper::get_store_stock_num($va['id'], 10);
                if ($ever_stock) {
                    $stock_num[] =$ever_stock.'箱';
                    $is_ever_stock[] = '是';
                }
                $ever_sell = store_helper::get_store_sell_num($va['id'], 10);
                if ($ever_sell) {
                    $sell_num[] = $ever_sell.'箱';
                }
            }

            //进货总箱数
            if($stock_num){
                $partner_lists['store_stock_goods_num'] = array_sum($stock_num).'箱';
            }else{
                $partner_lists['store_stock_goods_num'] = '0箱';
            }

            //出货总箱数
            if($sell_num){
                $partner_lists['store_sell_goods_num'] = array_sum($sell_num).'箱';
            }else{
                $partner_lists['store_sell_goods_num'] = '0箱';
            }

            //掌柜下单率 ，该合伙人下下订单的掌柜数除以该合伙人下所有的掌柜数，保留两位小数
            if($stock_num){
                $partner_lists['store_order_rate'] = count($stock_num)/count($store_list) * 100;
            }else{
                $partner_lists['store_order_rate'] = '0';
            }

            //所有设备总和
            $partner_lists['is_bind_device_store_num'] = device_helper::get_bind_device_count_by_agency_id($v);
            //订单总数
            $partner_lists['all_order_num'] = order_helper::get_order_num_by_progress(array(3, 5, 10) , 1, array('user_id' => $cur_dealer_info['id'], 'is_enable' => 1, 'status' => 1));

            // 待收款订单数
            $partner_lists['prepare_pay_order_num']      = order_helper::get_order_num_by_progress(1 , 1, array('user_id' => $cur_dealer_info['id'], 'is_enable' => 1, 'status' => 1));;
            // 待配送订单数
            $partner_lists['prepare_delivery_order_num'] = order_helper::get_order_num_by_progress(3 , 1, array('user_id' => $cur_dealer_info['id'], 'is_enable' => 1, 'status' => 1));;
            // 配送中订单数
            $partner_lists['in_delivery_order_num']      = order_helper::get_order_num_by_progress(5 , 1, array('user_id' => $cur_dealer_info['id'], 'is_enable' => 1, 'status' => 1));;
            // 配送完成订单数
            $partner_lists['finish_delivery_order_num']  = order_helper::get_order_num_by_progress(10 , 1, array('user_id' => $cur_dealer_info['id'], 'is_enable' => 1, 'status' => 1));;
            // 撤销订单订单数
            $partner_lists['cancel_order_num']    = order_helper::get_order_num_by_progress(15 , 1, array('user_id' => $cur_dealer_info['id'], 'is_enable' => 1, 'status' => 1));;

            $info = _uri('partner_bulletin', array('partner_id' => $v));
            if ($info) {
                // 更新
                _model('partner_bulletin')->update($info['id'], $partner_lists);
            } else {
                //新增日期
                $partner_lists['add_time'] = date('Y-m-d H:i:s');
                // 添加
                _model('partner_bulletin')->create($partner_lists);
            }
        }

    }
}